<?php
namespace modules\org;
use \modules\ui;
use \modules\auth\api as auth;
use \phiction\array_utils as arr;

class page
{
    static function mapping()
    {
        return [
            '/' => 'view',
            '/edit' => 'edit',
            '/delete' => 'delete',
        ];
    }

    static function view($q, $html, &$args)
    {
        $user = auth::validate($q, $args);
        $role = $user['role'];
        $is_admin = ($role === 'admin');

        $args['SUBTITLE'] = "组织";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html, [
            [ui::icon_label('编辑', 'edit' ), $is_admin ? "/org/edit"   : null],
            [ui::icon_label('注销', 'trash'), $is_admin ? "/org/delete" : null],
        ]);

        $info = api::view($q, $args);

        $codename = $info['codename'];
        $name     = $info['name'];
        $comment  = $info['comment'];

        return $html->mark(ui::card(ui::readonly_form($html, [
            ['text'    , '组织代号', $codename],
            ['text'    , '名称'    , $name    ],
            ['longtext', '备注'    , $comment ],
        ]), "组织"));
    }

    static function edit($q, $html, &$args)
    {
        $args['SUBTITLE'] = "编辑组织";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        $info = api::view($q, $args);

        $codename = $info['codename'];
        $name     = $info['name'];
        $comment  = $info['comment'];

        return $html->mark(ui::card(ui::form($html, '/api/org/edit',
            ui::icon_label("编辑", 'edit'), [
                ['text'    , '组织代号', $codename, 'readonly' => true ],
                ['text'    , '名称'    , $name    , 'name' => 'name'   ],
                ['longtext', '备注'    , $comment , 'name' => 'comment'],
            ], [
                [ui::icon_label('取消', 'angle-left'), '/org'],
            ], '/org'),
        "编辑组织"));
    }

    static function delete($q, $html, &$args)
    {
        $args['SUBTITLE'] = "注销组织";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return $html->mark(ui::card(['.',
            ['p', 'class' => 'error-message', "后果很严重。组织注销后无法恢复。请输入两次密码以确认注销。"],
            ui::form($html, '/api/org/delete',
                ui::icon_label('确认注销', 'trash'), [
                    ['password', '密码'    , 'name' => 'delete-password'],
                    ['password', '再次输入', 'name' => 'delete-password-again', 'data-same-value' => 'delete-password'],
                ], [
                    [ui::icon_label('取消', 'angle-left'), '/org'],
                ], '/'),
        ], "注销组织"));
    }
}

